code2.py
import gradio as gr
from code3_fetch_sql import query_answer # 引入外部函数
from code2_data_covert import excel_to_table,drop_table,get_table_names

tbl_names = get_table_names()
with gr.Blocks() as demo:

    with gr.Row():

        gr.Markdown('# 高考政策通')

    with gr.Row():

        with gr.Column():

            chatbot = gr.Chatbot(label='对话框', type="messages", height=500)

            question = gr.Textbox(label='请输入')

            clear_btn = gr.Button('clear')

        with gr.Column():

            gr.Markdown('### 数据库构建')

            datafile = gr.File(label='上传文件', type="filepath")

            dbname = gr.Textbox(label='数据库名称')

            build_btn = gr.Button('开始构建')

            gr.Markdown('### 数据库选择')

            dbchoose = gr.Dropdown(label='数据库名称',choices=tbl_names)

            with gr.Row():

                dbdelete_btn = gr.Button('删除')
    def user(query,history):
        history.append({'role':'user','content':query})
        return '',history
    def bot(history,sel_table):
        query = history[-1]['content']
        answer = query_answer(query)
        history.append({'role':'user','content':query})
        for chunk in answer:
            history[-1]['content'] +=chunk.choices[0].delta.content
            yield history
    def build_db(execl_file,dbname):
        info = excel_to_table(execl_file,dbname)
        if info['info'] == 'info':
            gr.Info(info['content'])
        else:
            gr.Warning(info['content'])
        tbl_names = get_table_names()
        return gr.update(choices=tbl_names,multiselcet=False)
    question.submit(user,inputs=[question,chatbot],outputs=[question,chatbot]).then(bot,[chatbot,dbchoose],chatbot)
    build_btn.click(build_db,inputs=[datafile,dbname],outputs = dbchoose)

demo.launch()

main.py
import gradio as gr
from code3_fetch_sql import query_answer # 引入外部函数
from code2_data_covert import excel_to_table,drop_table,get_table_names

tbl_names = get_table_names()
with gr.Blocks() as demo:

    with gr.Row():

        gr.Markdown('# 高考政策通')

    with gr.Row():

        with gr.Column():

            chatbot = gr.Chatbot(label='对话框', type="messages", height=500)

            question = gr.Textbox(label='请输入')

            clear_btn = gr.Button('clear')

        with gr.Column():

            gr.Markdown('### 数据库构建')

            datafile = gr.File(label='上传文件', type="filepath")

            dbname = gr.Textbox(label='数据库名称')

            build_btn = gr.Button('开始构建')

            gr.Markdown('### 数据库选择')

            dbchoose = gr.Dropdown(label='数据库名称',choices=tbl_names)

            with gr.Row():

                dbdelete_btn = gr.Button('删除')
    def user(query,history):
        history.append({'role':'user','content':query})
        return '',history
    def bot(history,sel_table):
        query = history[-1]['content']
        
        answer = query_answer(query,tbl_name=sel_table)
        history.append({'role':'assistant','content':''})
        for chunk in answer:
            history[-1]['content'] +=chunk.choices[0].delta.content
            yield history
    def build_db(execl_file,dbname):
        info = excel_to_table(execl_file,dbname)
        if info['info'] == 'info':
            gr.Info(info['content'])
        else:
            gr.Warning(info['content'])
    def delete_db(tbl_name):
        drop_table(tbl_name)
        gr.Info(f'数据表{tbl_name}删除成功！')
        tbl_names = get_table_names()
        return gr.update(choices=tbl_names,multiselect=False)
    question.submit(user,inputs=[question,chatbot],outputs=[question,chatbot]).then(bot,[chatbot,dbchoose],chatbot)
    build_btn.click(build_db,inputs=[datafile,dbname],outputs=dbchoose)
    dbdelete_btn.click(delete_db,inputs=dbchoose,outputs=dbchoose)
demo.launch()